package javaTest;

import java.util.Stack;

/*
用两个栈实现队列的功能
 */
public class StackToQueue {
    public static void main(String[] args) {
        StackToQueue stackToQueue = new StackToQueue();
        stackToQueue.push1(12);
        stackToQueue.push1(13);
        stackToQueue.push1(14);
        stackToQueue.push1(15);
        stackToQueue.pop1();
        stackToQueue.push1(16);
        stackToQueue.size1();
    }

    Stack<Integer> stackA = new Stack<>();
    Stack<Integer> stackB = new Stack<>();

    //add a element to queue
    public void push1(int x){
        if (stackA.empty()){
            stackA.push(x);
        }else{
            while (!stackA.empty())
                stackB.push(stackA.pop());
            stackA.push(x);
            while (!stackB.empty())
                stackA.push(stackB.pop());
        }
    }
    //remove the front element of the queue
    public int pop1(){
        if (!stackA.empty())
            return stackA.pop();
        return Integer.MIN_VALUE;
    }
    //print the front element of the queue, do not delete, just print
    public void peek1(){
        if (!stackA.empty())
            System.out.println(stackA.peek());
    }
    //print the size of the queue
    public void size1(){
        System.out.println(stackA.size());
    }
}
